package com.example.appinting;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.appinting.Beans.doctor;
import com.example.appinting.SQL.MysqlHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends AppCompatActivity {

    private EditText editTextUsername;
    private EditText editTextPassword;
    private Button buttonLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // 初始化控件
        editTextUsername = findViewById(R.id.et_user_name);
        editTextPassword = findViewById(R.id.et_psw);
        buttonLogin = findViewById(R.id.btn_login);

        // 设置按钮点击事件
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = editTextUsername.getText().toString();
                String password = editTextPassword.getText().toString();

                // 在这里处理登录逻辑，例如验证用户名和密码
                int patient_id = queryPatient(username,password);
                if (patient_id == 0 ){
                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    // 进行其他操作，如跳转到主界面
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    //传递用户名到用户界面
                    System.out.println("登录病人id为："+patient_id);
                    intent.putExtra("patient_id",Integer.toString(patient_id));
                    startActivity(intent);
                }
            }
        });
    }

    private int queryPatient(String phone_number,String pwd) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            System.out.println(phone_number+pwd);
            // 连接到 MySQL 数据库
            connection = MysqlHelper.getConnection();
            // 准备 SQL 语句
            String sql = "SELECT * FROM patient where phone_number = " + phone_number +" and pwd = " +  pwd;
            statement = connection.prepareStatement(sql);

            // 执行 SQL 语句
            resultSet = statement.executeQuery();

            // 处理查询结果
            if (resultSet.next()) {
                int patient_id = resultSet.getInt("patient_id");
                return patient_id;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接和资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return 0;
    }
}
